const app = getApp()
import prizeApi from '../../api/prizeApi'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prizeList: [],
    isMore: true,
    type: '',
    keyword: ''
  },
  pageData: {
    pageNum: 0,
    pageSize: 8
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {

    wx.showLoading({
      title: '加载中',
    })



    this.getAllPrize()


    wx.hideLoading()

  },

  onShow: function () {

    var updatePrize = wx.getStorageSync('updatePrize')
    wx.removeStorageSync('updatePrize')
    if (updatePrize) {
      for (var item of this.data.prizeList) {
        if (item._id == updatePrize.id) {
          for (let key in updatePrize) {
            item[key] = updatePrize[key]
          }
        }
      }
      this.setData({
        prizeList: this.data.prizeList
      })
    }
  },



  search(e) {

    this.data.keyword = e.detail.value

    this.pageData.pageNum = 0

    this.setData({
      prizeList: [],
      isMore: true
    })

    this.getAllPrize()
  },

  getAllPrize() {

    if (!this.data.isMore) {
      return
    }
    this.pageData.pageNum++

    var params = {
      pageNum: this.pageData.pageNum,
      pageSize: this.pageData.pageSize,

    }
    if (this.data.keyword) {
      params.content = this.data.keyword
    }

    wx.showLoading({
      mask: true
    })
    prizeApi.getAllPrize(params).then(res => {
      wx.hideLoading()
      console.log(res)
      if (res.length < this.pageData.pageSize) {
        this.setData({
          isMore: false
        })
      }

      var prizeList = this.data.prizeList.concat(res)
      this.setData({
        prizeList: prizeList
      })
    })
  },

  async copy(e) {
    var prize = e.currentTarget.dataset.prize
    var params = {
      content: prize.content,
      fileList: prize.fileList,
      imgList: prize.imgList,
      date: prize.date,
      type: prize.type,
      open: prize.open
    }
    params.openid = wx.getStorageSync('openid')
    params.avatarUrl = wx.getStorageSync('avatarUrl')
    params.nickname = wx.getStorageSync('nickname')
    params.status = 0
    params.top = 0
    wx.showLoading({
      mask:true
    })
    var data = await prizeApi.addPrize(params)
    wx.hideLoading()
    wx.showToast({
      title: '复制成功',
    })
  },

  toDetail(e) {
    var prize = e.currentTarget.dataset.prize

    wx.navigateTo({
      url: '/prize/prizeDetail/prizeDetail?prizeId=' + prize._id,
    })
  },

  toUpdate(e) {
    var prize = e.currentTarget.dataset.prize

    wx.navigateTo({
      url: `/prize/addPrize/addPrize?prizeId=${prize._id}&action=update`,
    })
  },

  toDelete(e) {
    var prize = e.currentTarget.dataset.prize
    var index = e.currentTarget.dataset.index
    wx.showModal({
      title: '提示',
      content: '确定删除？',
      success: async (res) => {
        if (res.confirm) {
          wx.showLoading({
            mask: true
          })
          var res = await prizeApi.deletePrize(prize._id)

          res = await wx.cloud.deleteFile({
            fileList: prize.fileList
          })

          this.data.prizeList.splice(index, 1)
          this.setData({
            prizeList: this.data.prizeList
          })
          wx.hideLoading()

          this.append()
        }
      }
    })
  },

  append() {

    var params = {
      pageNum: this.pageData.pageNum,
      pageSize: this.pageData.pageSize

    }
    if (this.data.keyword) {
      params.content = this.data.keyword
    }

    wx.showLoading({
      mask: true
    })
    prizeApi.getAllPrize(params).then(res => {
      wx.hideLoading()
      console.log(res)


      var prizeList = []
      for (var item of res) {
        var have = false
        for (var a of this.data.prizeList) {
          if (a._id == item._id) {
            have = true
          }
        }
        if (!have) {
          prizeList.push(item)
        }
      }

      var list = this.data.prizeList.concat(prizeList)
      this.setData({
        prizeList: list
      })
    })
  },

  async toTop(e) {
    var prize = e.currentTarget.dataset.prize
    let res = await prizeApi.updatePrize({
      id: prize._id,
      top: 1
    })
    wx.showToast({
      title: '置顶成功',
    })
  },

  async toControl(e) {
    var prize = e.currentTarget.dataset.prize

    var prize = e.currentTarget.dataset.prize
    let res = await prizeApi.updatePrize({
      id: prize._id,
      control: 1
    })
    wx.showToast({
      title: '管制成功',
    })
  },

  toOpen(e) {
    var prize = e.currentTarget.dataset.prize

    wx.navigateTo({
      url: '/prize/openPrize/openPrize?prizeId=' + prize._id,
    })
  },
  toJoin(e) {
    var prize = e.currentTarget.dataset.prize

    wx.navigateTo({
      url: '/prize/addJoin/addJoin?prizeId=' + prize._id,
    })
  },
  sendPrize(e) {
    var prize = e.currentTarget.dataset.prize

    wx.navigateTo({
      url: '/prize/sendPrize/sendPrize?prizeId=' + prize._id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },




  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getAllPrize()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})